package com.example.dzbsh.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.dzbsh.dao.DeptMapper;
import com.example.dzbsh.model.Dept;
import com.example.dzbsh.model.DeptExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DeptService {

    @Resource
    DeptMapper ddDeptMapper;

    public List<Dept> findByJigoumingchengma(String ppJigoumingchengma){
        DeptExample mmExample = new DeptExample();
        DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andJigoumingchengmaEqualTo(ppJigoumingchengma);
        return ddDeptMapper.selectByExample(mmExample);
    }

    public boolean add(Dept ppDept){
        return ddDeptMapper.insert(ppDept)==1;
    }

    public boolean modify(Dept ppDept){
        return ddDeptMapper.updateByPrimaryKey(ppDept)==1;
    }

    public Boolean delete(String ppDeptID) {
        return ddDeptMapper.deleteByPrimaryKey(ppDeptID) == 1;
    }

    public Dept findOne(String ppDeptID){
        return ddDeptMapper.selectByPrimaryKey(ppDeptID);
    }

    public List<Dept> findByParentdeptId(String ppParentdeptID){
        DeptExample mmExample = new DeptExample();
        DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andParentdeptidEqualTo(ppParentdeptID);
        return ddDeptMapper.selectByExample(mmExample);
    }
    
    public List<Dept> findAll() {
		DeptExample example = new DeptExample();
		example.createCriteria();
		example.setOrderByClause("ordernum asc");
		return ddDeptMapper.selectByExample(example);
	}

    // 获得最新排序号
    public int getMaxOrderNum(String ppParentDeptID) {
        Object mmMaxOrdernum = ddDeptMapper.getMaxOrderNum(ppParentDeptID);
        return mmMaxOrdernum == null ? 0 : Integer.parseInt(mmMaxOrdernum.toString());
    }
    
    public List<Dept> findByDuiwaimingcheng(String ppDuiwaimingcheng){
    	DeptExample mmExample = new DeptExample();
    	DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andDanweiduiwaimingchengEqualTo(ppDuiwaimingcheng);
        return ddDeptMapper.selectByExample(mmExample);
    }

    public List<Dept> findByXuliebianma(String ppXuliebianma){
        DeptExample mmExample = new DeptExample();
        DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andXuliebianmaEqualTo(ppXuliebianma);
        return ddDeptMapper.selectByExample(mmExample);
    }

    public List<Dept> findExitByXuliebianma(String ppDuiwaimingcheng,String ppDeptId) {
    	DeptExample mmExample = new DeptExample();
    	DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andDanweiduiwaimingchengEqualTo(ppDuiwaimingcheng).andDeptidNotEqualTo(ppDeptId);
        return ddDeptMapper.selectByExample(mmExample);
	}

    public List<Dept> findExitByDuiwaimingcheng(String ppXuliebianma,String ppDeptId) {
        DeptExample mmExample = new DeptExample();
        DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andXuliebianmaEqualTo(ppXuliebianma).andDeptidNotEqualTo(ppDeptId);
        return ddDeptMapper.selectByExample(mmExample);
    }

    public List<Dept> findExitByJigoumingchengma(String ppJigoumingchengma,String ppDeptId) {
        DeptExample mmExample = new DeptExample();
        DeptExample.Criteria criteria = mmExample.createCriteria();
        criteria.andJigoumingchengmaEqualTo(ppJigoumingchengma).andDeptidNotEqualTo(ppDeptId);
        return ddDeptMapper.selectByExample(mmExample);
    }
	public List<Map<String,Object>> findXiangxiByDeptId(String ppDeptid){
        return ddDeptMapper.findXiangxiByDeptId(ppDeptid);
    }

	// 获得传送进来的单位所有的下属单位
	public List<String> findXiajiByDeptId(String DeptId){
        List<String> mmDeptIdList = new ArrayList<>();
        List<Dept> mmDeptList = findByParentdeptId(DeptId);
        mmDeptIdList = findIdall(mmDeptList);
        return mmDeptIdList;
    }

    public List<String> findIdall (List<Dept> ppDeptList){
        List<String> mmDeptIdList = new ArrayList<>();
        for(int i=0;i<ppDeptList.size();i++){
            mmDeptIdList.add(ppDeptList.get(i).getDeptid());
            List<Dept> mmDeptList = findByParentdeptId(ppDeptList.get(i).getDeptid());
            if(mmDeptList.size()>0){
                List<String> mmxiashuIdList = findIdall(mmDeptList);
                mmDeptIdList.addAll(mmxiashuIdList);
            }
        }
        return mmDeptIdList;
    }

    // 获得传送进来的单位所有的下属单位
    public List<Dept> findCengjiByDeptId(String DeptId){

        List<Dept> mmDeptList = findByParentdeptId(DeptId);
        List<Dept> mmXiajiDeptList = findCengJi(mmDeptList,0);
        return mmXiajiDeptList;
    }

    public List<Dept> findCengJi (List<Dept> ppDeptList,int ppLunci){
        List<Dept> mmDeptList = new ArrayList<>();
        String ppPingjie = "";
        for(int a=0;a<ppLunci;a++){
            ppPingjie = ppPingjie+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        for(int i=0;i<ppDeptList.size();i++){
            ppDeptList.get(i).setDanweiduiwaimingcheng(ppPingjie+ppDeptList.get(i).getDanweiduiwaimingcheng());
            mmDeptList.add(ppDeptList.get(i));
            List<Dept> mmXiajiDeptList = findByParentdeptId(ppDeptList.get(i).getDeptid());
            if(mmXiajiDeptList.size()>0){
                List<Dept> mmXiashuList = findCengJi(mmXiajiDeptList,ppLunci+1);
                mmDeptList.addAll(mmXiashuList);
            }
        }
        return mmDeptList;
    }

}
